package com.example.skycode6.dao;

import com.example.skycode6.bean.AdminBean;
import com.example.skycode6.util.DBConnection;
import com.example.skycode6.util.Time;

import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AdminDao {
        DBConnection DBConn= new DBConnection();
    public boolean CheckLogin(String s1,String s2) throws SQLException {
        // conn对象代表数据路连接
        Connection conn = DBConn.getConn();
        Statement stmt = conn.createStatement();
        ResultSet rs = null;
        boolean OK = true;
        String sql = "select * from Admin where "+" AdminName='"+s1+"' and "+"AdminPwd='"+s2+"'";
        rs=stmt.executeQuery(sql);
        if(!rs.next()){
            OK = false;
        }
        return OK;
    }
    public List<AdminBean> getList(int page) {
        Connection conn = DBConn.getConn();
        String sql = "select * from admin limit " + (page - 1) * 10 + ",10";
        List<AdminBean> list = new ArrayList<>();
        try {
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                AdminBean adminBean = new AdminBean();
                adminBean.setAdminID(rs.getInt("AdminID"));
                adminBean.setAdminPwd(rs.getString("AdminPwd"));
                adminBean.setAdminType(rs.getString("AdminType"));
                adminBean.setLastLoginTime(Time.getDateTime());
                adminBean.setAdminName(rs.getString("AdminName"));
                list.add(adminBean);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    /**
     * @param adminBean 传入要更新的新闻对象，必须包含新闻id值
     * @return 受影响的行数
     */
    public int update(AdminBean adminBean) {
        Connection conn = DBConn.getConn();
        if (adminBean.getAdminID() > 0) {
            try {
                Statement stmt = conn.createStatement();
                String sql = "update admin set AdminPwd='" + adminBean.getAdminPwd() + "'"
                        + ",AdminNType='" + adminBean.getAdminType() + "'"
                        + ",LastLoginTime='" + Time.getDateTime() + "'"//更新时使用当前系统时间
                        + ",AdminName='" + "假管理员" + "'" //todo使用添加的管理员名称
                        + " where AdminID=" + AdminBean.getAdminID;
                return stmt.executeUpdate(sql);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return 0;
        }
        return 0;
    }

    public int add(AdminBean adminBean) {
        Connection conn = DBConn.getConn();
        try {// 使用预处理执行插入
            String sql = "insert into admin(AdminName,AdminPwd,AdminType,LastLoginTime) values(?,?,?,?)";
            PreparedStatement pmst = conn.prepareStatement(sql);
            pmst.setString(1, adminBean.getAdminName());
            pmst.setString(2, adminBean.getAdminPwd());
            pmst.setString(3, (new Date()).toString());
            pmst.setString(4, "临时管理员");//将第一个参数设置为标题内容
            return pmst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 删除功能
     */
    public int deleteNewsById(int adminID) {
        Connection conn = DBConn.getConn();
        try {
            String sql = "delete from admin where NewsID=?";
            PreparedStatement pmst = conn.prepareStatement(sql);
            pmst.setInt(1, adminID);
            return pmst.executeUpdate();
        } catch (SQLException e) {

        }
        return 0;
    }

    public int countPage() {
        Connection conn = DBConn.getConn();
        String sql = "select count(*) from news";
        try {
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery(sql);// 获取查询结果集
            if (rs.next()) {
                return (rs.getInt(1)/10) +1;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return 0;
    }
}
